package cn.datax.service.data.market.controller;

import cn.datax.common.base.BaseController;
import cn.datax.common.core.JsonPage;
import cn.datax.common.core.R;
import cn.datax.common.validate.ValidationGroups;
import cn.datax.service.data.market.api.dto.ApiWarnRuleDto;
import cn.datax.service.data.market.api.entity.ApiWarnRuleEntity;
import cn.datax.service.data.market.api.query.ApiWarnRuleQuery;
import cn.datax.service.data.market.api.vo.ApiWarnRuleVo;
import cn.datax.service.data.market.mapstruct.ApiWarnRuleMapper;
import cn.datax.service.data.market.service.ApiWarnRuleService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据API告警规则 前端控制器
 * </p>
 *
 * @author jimmy
 * @since 2022-07-27
 */
@Api(tags = {"数据API告警规则"})
@RestController
@RequestMapping("/apiWarnRule")
public class ApiWarnRuleController extends BaseController {
    @Autowired
    private ApiWarnRuleService apiWarnRuleService;

    @Autowired
    private ApiWarnRuleMapper apiWarnRuleMapper;

    /**
     * 添加
     * @param apiWarnRuleDto
     * @return
     */
    @ApiOperation(value = "添加信息", notes = "根据apiWarnRule对象添加信息")
    @ApiImplicitParam(name = "apiWarnRule", value = "详细实体apiWarnRule", required = true, dataType = "ApiWarnRuleDto")
    @PostMapping()
    public R saveApiWarnRule(@RequestBody @Validated({ValidationGroups.Insert.class}) ApiWarnRuleDto apiWarnRuleDto) {
        apiWarnRuleService.saveApiWarnRule(apiWarnRuleDto);
        return R.ok();
    }

    /**
     * 修改
     * @param apiWarnRuleDto
     * @return
     */
    @ApiOperation(value = "修改信息", notes = "根据url的id来指定修改对象，并根据传过来的信息来修改详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "apiWarnRule", value = "详细实体apiWarnRule", required = true, dataType = "ApiWarnRuleDto")
    })
    @PutMapping("/{id}")
    public R updateApiMask(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) ApiWarnRuleDto apiWarnRuleDto) {
        apiWarnRuleService.updateApiWarnRule(apiWarnRuleDto);
        return R.ok();
    }

    /**
     * 通过ID查询信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取详细信息", notes = "根据url的id来获取详细信息")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/rule/{id}")
    public R getApiWarnRuleById(@PathVariable String id) {
        ApiWarnRuleEntity apiWarnRuleEntity = apiWarnRuleService.getApiWarnRuleById(id);
        return R.ok().setData(apiWarnRuleMapper.toVO(apiWarnRuleEntity));
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public R deleteApiWarnRuleById(@PathVariable String id) {
        apiWarnRuleService.deleteApiWarnRuleById(id);
        return R.ok();
    }


    /**
     * 分页查询信息
     *
     * @param apiWarnRuleQuery
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "apiWarnRuleQuery", value = "查询实体apiWarnRuleQuery", required = true, dataTypeClass = ApiWarnRuleQuery.class)
    })
    @GetMapping("/page")
    public R getApiMaskPage(ApiWarnRuleQuery apiWarnRuleQuery) {
        QueryWrapper<ApiWarnRuleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(apiWarnRuleQuery.getApiName()), "api_name", apiWarnRuleQuery.getApiName());
        IPage<ApiWarnRuleEntity> page = apiWarnRuleService.page(new Page<>(apiWarnRuleQuery.getPageNum(), apiWarnRuleQuery.getPageSize()), queryWrapper);
        List<ApiWarnRuleVo> collect = page.getRecords().stream().map(apiWarnRuleMapper::toVO).collect(Collectors.toList());
        JsonPage<ApiWarnRuleVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return R.ok().setData(jsonPage);
    }

}
